import {Cache, cache_keys, env} from './utils';

export const login = async function(data, url, type, callback) {
    var callType = typeof callback;

    try {
        var res = await (() => {
            if (env.isWechat) {
                data.open_type = 'wechat';
                var openid = this.$route.query.openid || this.$route.query['?openid'];
                if (openid) {
                    data.open_id = openid;
                }
            }

            //return this.$http.post(url, data);
            return this.$http.get(url, data);
        })();

        var result = res.json();

        if (result.access_token) {
            result.access_token = result.token_type + ' ' + result.access_token;
            result.expires_in = result.expires_in || 315360000;  // token不过期
            result.expires = Date.now() + (result.expires_in - 300) * 1000;

            Cache.set(cache_keys.token, result, 0, null);

            this.$refs.button && this.$refs.button.done();

            if (callback) {
                if (callType === 'function') {
                    callback.call(this, result);
                    return null;
                } else if (callType === 'object') {
                    if (callback.onSuccess && typeof callback.onSuccess === 'function') {
                        callback.onSuccess.call(this, result);
                        return null;
                    }
                }
            }

            var from = this.$route.query.from;
            var source = this.$route.query.source;
            if (from) {
                window.location.href = from;
            } else if (source) {
                this.$router.replace(source);
            } else {
                this.$router.replace({name: 'index'});
            }
        } else {

            this.$refs.button && this.$refs.button.done();

            if (callback) {
                if (callType === 'function') {
                    callback.call(this, result);
                    return null;
                } else if (callType === 'object') {
                    if (callback.onError && typeof callback.onError === 'function') {
                        callback.onError.call(this, result);
                        return null;
                    }
                }
            }

            if (type === 0) {
                this.$Alert('用户名或密码输入错误');
            } else if (type === 1) {
                this.$Alert('验证码错误');
            }
        }
    } catch (e) {
        console.error(e);
        this.$refs.button && this.$refs.button.done();

        if (callback && typeof callback === 'function') {
            callback.call(this, {
                status: false
            });
            return null;
        }

        if (type === 0) {
            this.$Alert('用户名或密码输入错误');
        } else if (type === 1) {
            this.$Alert('验证码错误');
        }
    }
};

export const quickLogin = async function (callback) {
    var data = {
        grant_type: 'sms_token',
        access_token: this.sms_token,
        mobile: this.mobile,
        code: this.code,
        type: 'direct'
    };

    //var url = this.$configs.baseUrl + 'api/oauth/sms';
    var url = './static/json/user/login.json';

    await login.call(this, data, url, 1, callback);
};

export const openidLogin = async function (callback) {
    var data = {};

    var url = this.$configs.baseUrl + 'api/oauth/quicklogin';

    await login.call(this, data, url, 2, callback);
};



export const logout = function (name) {
    Cache.remove(cache_keys.token);
    this.$router.forward({name: name || 'index'});
};

export const getOpenid = function () {
    var from = this.$route.query.from;
    var source = this.$route.query.source;
    var pathname = window.location.pathname;

    var query = { type: 'oauth' };
    if (from) {
        query.from = from;
    } else if (source) {
        query.source = source;
    }

    let url = this.$router.stringifyPath({ name: this.$route.name, query });
    if ( this.$router.mode === 'hash' ) url = '#!' + url;
    console.log(url);
    url = decodeURIComponent(url);

    url = this.$configs.baseUrl + 'oauth/wxlogin?redirect_url=' + encodeURIComponent(window.location.origin + pathname + url + '&');

    window.location.href = url;
};